% This matlab script takes the filter parameters as input which is given
% in 'f' and 'choice'(gain for respective frequency components) and order N of the filter gives the
% coefficents of the required filter.

%  This method is implemeted throught the below three stages 
%  1.Define suitable FIR filter
%  2.Read the Input sound and store the data
%  3.Pass through the filter(use overlapaddmethod) and play the output

% Firstly the sound file is read into an input array using wavread
% function, then it is passed through the filter using the defined function
% fftfilt() 

%  The modified output audio file is player after filtering the input audio
%  file.

function [output,fs]=overlapadd(inputstr,ch,standard)
%clc
%clear all
%###################Define filter #########################################
frequency = [0 0.001875 0.0053125 0.0096875 0.01875 0.03125 0.09375 0.1875 0.375 0.4375 0.5 1];
%m = [4 4 4 4 8 8 8 0 0 0 0 0];
%m=[0 0 0 0 0 0 1 3 5 7 9 11];
%bass=[1 0.4 3.01 3.01 1.8836 1.20225 0.63 0.398 0.301 0.275 0.275 1]; % presetting the bass magnitude levels
%flat=[1 1 1 1 1 1 1 1 1 1 1 1];%presetting the flat magnitude levels
%headphones=[1 1.7378 3.5892 1.8836 0.6839 0.7499 1.2023 1.7378 3.02 4.3652 5.2481 1]; %presetting the headphones magnitude level 

N=256;

choice=zeros(1,length(ch));
for i=1:length(ch)
choice(i)=(10^(ch(i)/20));
end
filtercoeff = fir2(N,frequency,choice);
[h,w] = freqz(filtercoeff,1,[]);
figure(1);
plot(frequency,choice,w/pi,(abs(h)))
legend('Ideal','fir2 Designed')
title('Comparison of Frequency Response Magnitudes')
grid on;


%######read the input sound signal in .wav###################################
%s='file2.wav';
[input,fs]=wavread(inputstr);
%wavplay(input,fs);
input=input(:,1);
figure(2);

subplot(2,1,2);
plot(input);%plotting input signal in time domain
xlabel('time');
ylabel('amplitude');
title('input signal in time domain');  
n=length(input);
[inputf,w]=freqz(input,1,n);
subplot(2,1,1);
plot(w/pi,(abs(inputf)));% plotting input signal in frequency domain 
title('input signal in frequncy domain');
xlabel('normalized frequency');
ylabel('magnitude');

%###########pass through the filter######################################



%output=ovlam(input,filtercoeff,128); %chosen 128 as blocklength
output=fftfilt(filtercoeff,input);
figure(3);
subplot(2,1,2);
plot(output);
xlabel('time');
ylabel('amplitude');
title('output signal in time domain');
[outputf,w]=freqz(output,1,n);
subplot(2,1,1);
plot(w/pi,(abs(outputf)));
xlabel('normalized frequency');
ylabel('magnitude');
title('output signal in frequency domain');

%######## play the output#########################

%wavplay(output,fs);

%######## delta signal #########################
if(strcmp(standard,'noname') == 0)
[std,fs]=wavread(standard);
std=std(:,1);
output=output(:,1);
t=length(output)-length(std);
std1=[std ; zeros(t,1)];
length(std1);
y=abs(output)-abs(std1);
figure(4);
plot(y);
title('Delta Signal');
xlabel('Time');
ylabel('Amplitude');
end